package com.example.Spshare;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.example.testandroid.R;

/**
 * Created by install on 14/12/9.
 */
public class SPActivity extends Activity {
    String key;
    String val;
    EditText et1;
    EditText et2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sp_activity);

        Context context = null;
        try{
            context = createPackageContext("com.example.test",Context.CONTEXT_IGNORE_SECURITY);
        }catch (Exception e){
            e.printStackTrace();
        }
        final SharedPreferences sp = context.getSharedPreferences("SPShare",MODE_WORLD_READABLE);
        final SharedPreferences.Editor editor = sp.edit();

        Button svBtn = (Button)findViewById(R.id.svBtn);
        Button reBtn = (Button)findViewById(R.id.reBtn);
        et1 = (EditText)findViewById(R.id.et1);
        et2 = (EditText)findViewById(R.id.et2);

        svBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                key = et1.getText().toString();
                val = et2.getText().toString();
                editor.putString(key,val);
                editor.commit();
                Toast.makeText(SPActivity.this, "保存成功", Toast.LENGTH_SHORT).show();

            }
        });


        reBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                key = et1.getText().toString();
                val = sp.getString(key,"no_value");
                et2.setText(val);
            }
        });
    }
}